
clear

cd "D:\data_replication"

import delimited estimation\4_demand_estimation\4_cobb_douglas_weights\Y_summary.csv


// Create pc8, which replaces synthetic categories by 0
//==============================================================================

gen pc8 = substr(pc8plus, 1, 8)
gen ll = length(pc8plus)
replace pc8 = "00000000" if ll < 8                                               


// Create shares by pc8
//==============================================================================

sort year quarter declarant pc8 
by year quarter declarant pc8: egen share_pc8 = sum(share)
by year quarter declarant pc8: keep if _n == 1 


// Adjust shares such that sum = 1
//==============================================================================

sort year quarter declarant
by year quarter declarant: egen share_total = sum(share_pc8)
replace share_pc8 = share_pc8 / share_total                                      


// Format and save
//==============================================================================

drop pc8plus share ll share_total
order pc8 year quarter declarant
sort pc8 year quarter declarant

gen byte notnumeric = real(pc8)==.                                              // One nonnumeric category
drop if notnumeric == 1
drop notnumeric

export delimited using estimation\4_demand_estimation\4_cobb_douglas_weights\Y_summary_8d.csv, replace


